<?php
// +----------------------------------------------------------------------
// | Author: huangzhen_20210406
// +----------------------------------------------------------------------

namespace app\common\model;
use think\facade\Db;

class ServerLss extends Common
{
    //---------------------------------------------后端---------------------------------------------------------------
    /**
     * 返回layui的table所需要的格式
     * @param $post
     * @param $user_id
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function tableDataAdmin($post)
    {
        $list_rows  = $post['limit'] ?? config('paginate.list_rows');
        $page       = $post['page'] ?? config('paginate.page');

        $where =[];
        if(isset($post['username']) && !empty($post['username'])){
            $where[] = ['username', 'like', '%'. trim($post['username']).'%'];
        }
        if(isset($post['type']) && strlen($post['type'])>0){
            $where[] = ['ae.type', '=', $post['type']];
        }
        if(isset($post['card_num']) && !empty($post['card_num'])){
            $where[] = ['ae.card_num', '=', $post['card_num']];
        }
        if(isset($post['datetime']) && !empty($post['datetime'])){
            $time_list = explode('_' , $post['datetime']);
            $start_time = trim($time_list[0]) . " 00:00:00";
            $end_time   = trim($time_list[1]) . " 23:59:59";
            $where[] = ['ae.ctime', 'between', "{$start_time},{$end_time}"];
        }
        $list = Db::name('server_lss')->alias('ae')
                ->leftJoin('user u','u.id=ae.user_id')
                ->field('username,ae.*')
                ->order('ctime desc')
                ->where($where)
                ->paginate(['list_rows' => $list_rows, 'page' => $page])
                ->toArray();

        return $result = [
            'code'  => 0,
            'msg'   => lang('get info success'),
            'data'  => $list['data'],
            'count' => $list['total'],
        ];
    }

    public function tableDataAdminMax($post)
    {
        $list_rows  = $post['limit'] ?? config('paginate.list_rows');
        $page       = $post['page'] ?? config('paginate.page');

        $declined_max_amount = getSetting('declined_max_amount');
        $where =[];
        $where[] = ['ae.is_action', '=', 0];
        $where[] = ['ae.tran_amount', '>', $declined_max_amount];
        if(isset($post['username']) && !empty($post['username'])){
            $where[] = ['username', 'like', '%'. trim($post['username']).'%'];
        }
        if(isset($post['type']) && strlen($post['type'])>0){
            $where[] = ['ae.type', '=', $post['type']];
        }
        if(isset($post['card_num']) && !empty($post['card_num'])){
            $where[] = ['ae.card_num', '=', $post['card_num']];
        }
        if(isset($post['datetime']) && !empty($post['datetime'])){
            $time_list = explode('_' , $post['datetime']);
            $start_time = trim($time_list[0]) . " 00:00:00";
            $end_time   = trim($time_list[1]) . " 23:59:59";
            $where[] = ['ae.ctime', 'between', "{$start_time},{$end_time}"];
        }
        $list = Db::name('server_lss')->alias('ae')
            ->leftJoin('user u','u.id=ae.user_id')
            ->field('username,ae.*')
            ->order('ctime desc')
            ->where($where)
            ->paginate(['list_rows' => $list_rows, 'page' => $page])
            ->toArray();

        return $result = [
            'code'  => 0,
            'msg'   => lang('get info success'),
            'data'  => $list['data'],
            'count' => $list['total'],
        ];
    }
}